package com.work.cos.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * 用于时间的各种格式化
 * 
 */
public class DateUtil
{
	private static Calendar			calendar			= Calendar.getInstance(java.util.Locale.CHINA);
	private static SimpleDateFormat	dateFormat_yyyyMMdd	= new SimpleDateFormat("yyyy-MM-dd");

	private static SimpleDateFormat	detailTimeFormat	= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat	detailMilliTimeFormat	= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S ");
	private static SimpleDateFormat	dateFormat_yyyyMM	= new SimpleDateFormat("yyyy-MM");

	private static SimpleDateFormat	dateFormat_ddMMyyyy	= new SimpleDateFormat("dd/MM/yyyy");
	private static SimpleDateFormat	chineseDateFormat	= new SimpleDateFormat("yyyy年MM月dd日");
	private static SimpleDateFormat chineseDateFormat2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");

	/**
	 * 将"yyyy-MM"型字符串转换为date对象。
	 * 
	 * @param dateString:满足"yyyy-MM"的字符串
	 * @return
	 */
	public static Date getYMDate(String dateString)
	{
		try
		{
			return dateFormat_yyyyMM.parse(dateString);
		}
		catch (ParseException e)
		{
			return null;
		}

	}

	
	public static Date getYMDateForNotice(String dateString)
	{
		try
		{
			return chineseDateFormat2.parse(dateString);
		}
		catch (ParseException e)
		{
			return null;
		}

	}
	

	/**
	 * 将"yyyy-MM-dd"型字符串转换为date对象。
	 * 
	 * @param dateString:满足"yyyy-MM-dd"的字符串
	 * @return
	 */
	public static Date getYMDDate(String dateString)
	{
		try
		{
			return dateFormat_yyyyMMdd.parse(dateString);
		}
		catch (ParseException e)
		{
			return null;
		}
	}
	

	/**
	 * 获取指定年月日的Date
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static Date getDate(int year, int month, int day)
	{
		calendar.clear();
		calendar.set(year, month - 1, day);
		return calendar.getTime();
	}

	/**
	 * 判断该date是星期几.
	 * 
	 * @param date:指定date
	 * @return 返回1-7，1代表Sunday,7代表Saturday.
	 */
	public static int getWeekday(Date date)
	{
		calendar.clear();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 判断该”year年month月date日“是星期几.
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return 返回1-7，1代表Sunday,7代表Saturday.
	 */
	public static int getWeekday(int year, int month, int day)
	{
		calendar.clear();
		calendar.set(year, month - 1, day);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 判断该date是否是星期六
	 * 
	 * @param date:指定date
	 */
	public static boolean isSaturday(Date date)
	{
		return getWeekday(date) == Calendar.SATURDAY;
	}

	/**
	 * 判断该date是否是星期日
	 * 
	 * @param date:指定date
	 */
	public static boolean isSunday(Date date)
	{
		return getWeekday(date) == Calendar.SUNDAY;
	}

	/**
	 * 判断该date是否是周末
	 * 
	 * @param date:指定date
	 */
	public static boolean isWeekend(Date date)
	{
		return isSaturday(date) || isSunday(date);
	}

	/**
	 * 获取date对象的 "yyyy-MM"字符串形式
	 * 
	 * @param date
	 * @return
	 */
	public static String getDateYMString(Date date)
	{
		return dateFormat_yyyyMM.format(date);

	}

	/**
	 * 获取date对象的 "yyyy年MM月dd日"字符串形式
	 * 
	 * @param date
	 * @return
	 */
	public static String getChineseDateString(Date date)
	{
		if (date != null)
		{
			return chineseDateFormat.format(date);
		}
		return "";

	}
	
	public static String getChineseDateString2(Date date)
	{
		if (date != null)
		{
			return chineseDateFormat2.format(date);
		}
		return "";

	}
	
	/**
	 * 将"yyyy-MM-dd hh:mm:ss"型字符串转换为date对象。
	 * @param
	 * @return
	 */
	public static Date getDateTime(String dateTimeString)
	{
		try
		{
			return detailTimeFormat.parse(dateTimeString);
		}
		catch (ParseException e)
		{
			return null;
		}
	}

	/**
	 * 将date对象转换为"yyyy-MM-dd hh:mm:ss"型字符串。
	 * @param
	 * @return
	 */
	public static String getDetailMilliTimeString(Date time)
	{
		return detailMilliTimeFormat.format(time);
	}
	
	/**
	 * 将date对象转换为"yyyy-MM-dd hh:mm:ss"型字符串。
	 * @param
	 * @return
	 */
	public static String getDetailTimeString(Date time)
	{
		return detailTimeFormat.format(time);
	}
	

	/**
	 * 获取date对象的 "yyyy-MM-dd"字符串形式
	 * @param date 
	 * @return
	 */
	public static String getDateString(Date date)
	{
		if (date == null)
			return "";
		return dateFormat_yyyyMMdd.format(date);
	}

	/**
	 *  获取date对象的 "ddMMyyyy"字符串形式 
	 * @param date
	 * @return
	 */
	public static String getDMYDate(Date date)
	{
		if (date == null)
			return "";
		return dateFormat_ddMMyyyy.format(date);
	}
	
	
	/**
	 * 获取Date对象中 年份
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date)
	{
		calendar.clear();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 获取Date对象中 月份(月份以现实为准,1代表一月)
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date)
	{
		calendar.clear();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1; // JAVA中一月从0开始，为避免混淆，故+1
	}

	/**
	 * 获取Date对象中 天
	 * 
	 * @param date
	 * @return
	 */
	public static int getDay(Date date)
	{
		calendar.clear();
		calendar.setTime(date);
		return calendar.get(Calendar.DATE);
	}
	

	/**
	 * 返回指定指定月份 天数
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getDaysCount(int year, int month)
	{
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1); // Calendar中month是从0开始
		return calendar.getActualMaximum(Calendar.DATE);
	}

	/**
	 * 但指定日期进行加减天数
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date addDays(Date date, int offset)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + offset);// 让日期加位移量
		return calendar.getTime();
	}

	/**
	 * 指定日期进行加减小时
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date addHours(Date date, int offset)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + offset);
		return calendar.getTime();
	}

	/**
	 * 但指定日期进行减天数
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date reduceDays(Date date, int offset)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - offset);// 让日期加位移量
		return calendar.getTime();
	}

	/**
	 * 指定日期进行减小时
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date reduceHours(Date date, int offset)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - offset);
		return calendar.getTime();
	}

	/**
	 * 指定日期进行减分钟
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date reduceMinute(Date date, int offset)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - offset);
		return calendar.getTime();
	}

	/**
	 * 指定日期进行减月份
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date reduceMonth(Date date, int offset)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - offset);
		return calendar.getTime();
	}

	/**
	 * 获取Date对象中 分钟 LK0142
	 * 
	 * @param date
	 * @return
	 */
	public static int getMinute(Date date)
	{
		calendar.clear();
		calendar.setTime(date);
		return calendar.get(Calendar.MINUTE);
	}

	/**
	 * 获取Date对象中 小时 LK0142
	 * 
	 * @param date
	 * @return
	 */
	public static int getHour(Date date)
	{
		calendar.clear();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR_OF_DAY);
	}



	/**
	 * 获取两个日期的间隔天数
	 */
	public static Long getDaysBetween(Date startDate, Date endDate)
	{
		Calendar fromCalendar = Calendar.getInstance();
		fromCalendar.setTime(startDate);
		fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
		fromCalendar.set(Calendar.MINUTE, 0);
		fromCalendar.set(Calendar.SECOND, 0);
		fromCalendar.set(Calendar.MILLISECOND, 0);

		Calendar toCalendar = Calendar.getInstance();
		toCalendar.setTime(endDate);
		toCalendar.set(Calendar.HOUR_OF_DAY, 0);
		toCalendar.set(Calendar.MINUTE, 0);
		toCalendar.set(Calendar.SECOND, 0);
		toCalendar.set(Calendar.MILLISECOND, 0);

		return (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24);
	}
	
	/**
	 * 获取两个日期的间隔小时
	 */
	public static Long getHoursBetween(Date startDate, Date endDate)
	{
		Calendar fromCalendar = Calendar.getInstance();
		fromCalendar.setTime(startDate);
		fromCalendar.set(Calendar.MINUTE, 0);
		fromCalendar.set(Calendar.SECOND, 0);
		fromCalendar.set(Calendar.MILLISECOND, 0);

		Calendar toCalendar = Calendar.getInstance();
		toCalendar.setTime(endDate);
		toCalendar.set(Calendar.MINUTE, 0);
		toCalendar.set(Calendar.SECOND, 0);
		toCalendar.set(Calendar.MILLISECOND, 0);

		return (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60);
	}


	/**
	 * 获取两个整数的间隔小时数
	 */
	public static int getHoursBetween(int starth, int endh)
	{
		if (endh < starth)
		{
			return 24 + endh - starth;
		}
		return endh - starth;
	}

	/**
	 * 获取两个整数的间隔分钟数
	 */
	public static int getMinutessBetween(int startm, int endm)
	{
		if (endm < startm)
		{
			return 60 + endm - startm;
		}
		return endm - startm;
	}
	
	/**
	 * 获取制定格式的当前时间
	 * 
	 * @param format
	 * @return
	 */
	public static String getFormatNow(String format){
	    SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
	    return df.format(new Date());// new Date()为获取当前系统时间
	}

	
	/**
	 * 取到TimeStamp的当前时间
	 * 
	 * @return Now TimeStamp
	 */
	public static Timestamp getTimeStampNow(){
		return new Timestamp(Calendar.getInstance().getTimeInMillis());
				
	}

	/**输入时间得到几天后的时间
	 * @param format
	 * @param dateStart
	 * @param day
	 * @return
	 */
	public static Date delayTime(String format,Date dateStart, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateStart);
		cal.add(Calendar.DATE, day);
		Date newDate = cal.getTime();
		return newDate;
	}
	
	/**
	 * 根据给定的格式返回当前的时间
	 * 
	 * @param format
	 * @return
	 */
	public static String getTimeByFormat(Date date,String format) {
		SimpleDateFormat  sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	public static String removeDateDot(String dateString) {
		   String createTime = "";
	        int index = dateString.indexOf(".");
	        if(index!= -1) {
	        	createTime = dateString.substring(0, index);
			} else {
				createTime = dateString;
			}
	        return createTime;
	}
	public static Date  String2Date(String str) throws ParseException {
		return dateFormat_yyyyMMdd.parse(str);
	}
	
	/**
	 * 获取日期几分钟前，几年前
	 * 方法名：getTimeFormat<BR>
	 * 创建人：Grace <BR>
	 * 时间：2014年11月11日-下午10:27:54 <BR>
	 * @param startTime
	 * @return String<BR>
	 * @exception <BR>
	 * @since  1.0.0
	 */
	public static String getTimeFormat(Date startTime){
		try{
			long startTimeMills = startTime.getTime();
			long endTimeMills = System.currentTimeMillis();
			long diff = (endTimeMills - startTimeMills)/1000;//秒
			long day_diff  = (long) Math.floor(diff/86400);//天
			StringBuffer buffer = new StringBuffer();
			if(day_diff<0){
				return "[error],时间越界...";
			}else{
				if(day_diff==0 && diff<60){
					if(diff==0)diff=1;
					buffer.append(diff+"秒前");
				}else if(day_diff==0 && diff<120){
					buffer.append("1 分钟前");
				}else if(day_diff==0 && diff<3600){
					buffer.append(Math.round(Math.floor(diff/60))+"分钟以前");
				}else if(day_diff==0 && diff<7200){
					buffer.append("1小时前");
				}else if(day_diff==0 && diff<86400){
					buffer.append(Math.round(Math.floor(diff/3600))+"小时前");
				}else if(day_diff==1){
					buffer.append("1天前");
				}else if(day_diff<7){
					buffer.append(day_diff+"天前");
				}else if(day_diff <30){
					buffer.append(Math.round(Math.ceil( day_diff / 7 )) + " 星期前");
				}else if(day_diff >=30 && day_diff<=179 ){
					buffer.append(Math.round(Math.ceil( day_diff / 30 )) + "月前");
				}else if(day_diff >=180 && day_diff<365){
					buffer.append("半年前");
				}else if(day_diff>=365){
					buffer.append(Math.round(Math.ceil( day_diff /30/12))+"年前");
				}
			}
			return buffer.toString();
		}catch(Exception ex){
			return "";
		}
	}	
	
	/**
	 * 获取某段时间内的所有日期 
	 * 方法名：findDates
	 * 创建人：Grace 
	 * 时间：2015年11月11日-下午10:27:54
	 * @param dBegin,dEnd
	 * @return List<Date>
	 * @exception 
	 * @since  1.0.0
	 */
	public static List<Date> findDates(Date dBegin, Date dEnd) {  
        List lDate = new ArrayList();  
        lDate.add(dBegin);  
        Calendar calBegin = Calendar.getInstance();  
        // 使用给定的 Date 设置此 Calendar 的时间    
        calBegin.setTime(dBegin);  
        Calendar calEnd = Calendar.getInstance();  
        // 使用给定的 Date 设置此 Calendar 的时间    
        calEnd.setTime(dEnd);  
        // 测试此日期是否在指定日期之后    
        while (dEnd.after(calBegin.getTime())) {  
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量    
            calBegin.add(Calendar.DAY_OF_MONTH, 1);  
            lDate.add(calBegin.getTime());  
        }  
        return lDate;  
    }  
}
